<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class CollectController extends Controller
{
    /**
     *
     */
    public function collect()
    {

        //插入单条详情
        // $code = $this->insertDetail();
       
        // $this->collectImage();

        $urls = $this->getList();
        foreach($urls as $k=>$v){
            foreach($v as $k=>$url){
                $this->insertDetail($url);
            }
        }

    }

    public function getList()
    {
        $data = [];
        for($i=0;$i<2;$i++){
            //要获取的列表网页
            $url = 'http://www.yougou.com/f-0-IHY_MLU_YR5_FSU_4TH-0-'.$i.'.html';

            //curl初始化
            $ch = curl_init($url);
            //设置
            curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
            curl_setopt($ch,CURLOPT_HTTPHEADER,[
                'Host: www.yougou.com',
                'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0',
                'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
                'Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
                'Accept-Encoding: deflate',
                'Cookie: aliyungf_tc=AQAAALzoLmuX1QwAY4HNfPwwCFcqvq45; yg_history_goods=100489706; admanageType=A; yg_sid=f23d381d-7cc1-47f6-a2d9-db20ad9fe27c; JSESSIONID=5C75CAC97CCC2A7A7E68E4FFBEADC14B; __utma=95907011.1786411734.1479304082.1479304082.1479304082.1; __utmb=95907011.1.10.1479304082; __utmc=95907011; __utmz=95907011.1479304082.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __yga=%28direct%29.%28direct%29; NTKF_PAGE_MANAGE=%7B%22m%22%3A%5B%7B%2282902%22%3A40997%7D%5D%2C%22t%22%3A%2221%3A48%3A19%22%7D; bdshare_firstime=1479304084525; NTKF_CACHE_DATA=%7B%22debug%22%3A%7B%7D%2C%22uid%22%3A%22kf_9923_ISME9754_guest546E624F-6173-75%22%2C%22tid%22%3A%221479304085306000598%22%2C%22fsid%22%3A%221479304085034000103%22%7D; NTKF_T2D_CLIENTID=guest546E624F-6173-752E-C715-3C48E91238D8; vizNprd=1%7C1479390487032',
                'Connection: close',
                'Upgrade-Insecure-Requests: 1',
                'Cache-Control: max-age=0'
                ]);
            $code = curl_exec($ch);
            // $data[] = $code;
            // dd($data);
            if(!empty($code)){
                preg_match_all('/<div class="hd">.*<a target="_blank" href="(.*)">.*<\/a>.*<\/div>/isU',$code,$tmp);
                // dd($tmp);
                $data[] = $tmp[1];
            }
        }
        // dd($data);
        return $data;
    }

    //获取详情
    public function getDetail($url)
    {
        // $url = 'http://www.yougou.com/c-adidas/sku-jpe41-100489706.shtml#ref=list&po=list';
        //curl初始化
        $ch = curl_init($url);
        //设置
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch,CURLOPT_HTTPHEADER,[
            'Host: www.yougou.com',
            'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0',
            'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
            'Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
            'Accept-Encoding: deflate',
            'Cookie: aliyungf_tc=AQAAALzoLmuX1QwAY4HNfPwwCFcqvq45; yg_history_goods=100489706; admanageType=A; yg_sid=f23d381d-7cc1-47f6-a2d9-db20ad9fe27c; JSESSIONID=5C75CAC97CCC2A7A7E68E4FFBEADC14B; __utma=95907011.1786411734.1479304082.1479304082.1479304082.1; __utmb=95907011.1.10.1479304082; __utmc=95907011; __utmz=95907011.1479304082.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __yga=%28direct%29.%28direct%29; NTKF_PAGE_MANAGE=%7B%22m%22%3A%5B%7B%2282902%22%3A40997%7D%5D%2C%22t%22%3A%2221%3A48%3A19%22%7D; bdshare_firstime=1479304084525; NTKF_CACHE_DATA=%7B%22debug%22%3A%7B%7D%2C%22uid%22%3A%22kf_9923_ISME9754_guest546E624F-6173-75%22%2C%22tid%22%3A%221479304085306000598%22%2C%22fsid%22%3A%221479304085034000103%22%7D; NTKF_T2D_CLIENTID=guest546E624F-6173-752E-C715-3C48E91238D8; vizNprd=1%7C1479390487032',
            'Connection: close',
            'Upgrade-Insecure-Requests: 1',
            'Cache-Control: max-age=0'
            ]);

        $code = curl_exec($ch);
        return $code;
    }

    //插入商品详情
    public function insertDetail($url)
    {
        $code = $this->getDetail($url);
        // dd($code);

        if(!empty($code)){
            //标题
            preg_match_all("/<h1>(.*)<\/h1>/isU",$code,$tmp1);
            //获取主图
            preg_match_all('/picBigUrl="(.*)".*picLargeUrl="(.*)".*src="(.*)"/isU',$code,$tmp2);
            //价格
            preg_match_all("/<del>&yen; (.*) <\/del>/isU",$code,$tmp3);
            //详情
            preg_match_all('/<div id="contentDetail">(.*)<\/div>/isU',$code,$tmp4);
        }

        //插入商品主表
        $id = DB::table('shop_goods')->insertGetId([
                'title'=>$tmp1[1][0],
                'price'=>$tmp3[1][0],
                'cate_id'=>rand(1,7),
                'detail'=>$tmp4[1][0],
                'kucun'=>rand(100,10000),
                'color'=>'黑色@@白色@@灰色@@蓝色@@红色',
                'size'=>'M@@L@@XL@@XLL'
            ]);
        // dd($id);
        //插入商品图片表
        if($id){
            $data = [];
            $count = count($tmp2);
            for($i=1;$i<$count;$i++){
                foreach($tmp2[$i] as $k=>$v){
                    
                    $data[$k][$i] = $v;
                }
            }

            // dd($data);
            $goopic = [];
            //获取图片 及 要插入的数据
            foreach($data as $k=>$v){
                // 文件夹的名称
                $dir = './uploads/'.date('Ymd').'/';
                if(!is_dir($dir)){
                    mkdir($dir);
                }
                 //文件名称
                $fileName = time().rand(10000,99999);
                 
                // dd($v);
                //定义大中小图片名称数组
                $arr = [1=>'_b',2=>'_l',3=>'_t'];
                //获取$v内元素个数
                $num = count($v);
                for($j=1;$j<=$num;$j++){
                    //文件后缀
                    $res = pathinfo($v[$j]);
                    $str = $res['extension'];//jpg?2
                    preg_match('/\w+/',$str,$suffix);
                    $suffix = $suffix[0]; //jpg
                    //拼接路径
                    $path = $dir.$fileName.$arr[$j].'.'.$suffix;
                    //调用采集图片
                    $this->collectImage($v[$j],$path);
                    //插入商品图片表
                    if($j == 1){
                        $t = [];
                        $t['goods_id'] = $id;
                        $t['path'] = ltrim($path,'.');
                        $goopic[] = $t;
                    }
                }

            }
            // dd($goopic);
        }

        $res = DB::table('shop_goopics')->insert($goopic);
        if($res){
            echo '采集成功';
        }
    }
    

    //采集图片
    public function collectImage($url,$path)
    {
        //获取网址
        $info = file_get_contents($url);
        //输出图片
        file_put_contents($path,$info);
    }




}
